package com.itc.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huawei.shade.org.joda.time.LocalDateTime;
import com.itc.common.core.domain.R;
import com.itc.dispatch.common.Constents;
import com.itc.dispatch.domain.AlarmConfig;
import com.itc.dispatch.domain.SysAlarmType;
import com.itc.dispatch.domain.dto.SysAlarmTypePageDto;
import com.itc.dispatch.domain.vo.SysAlarmTypeVo;
import com.itc.dispatch.service.AlarmConfigService;
import com.itc.dispatch.service.SysAlarmTypeService;
import com.itc.dispatch.mapper.SysAlarmTypeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author 智少
* @description 针对表【sys_alarm_type】的数据库操作Service实现
* @createDate 2024-07-24 14:49:45
*/
@Service
public class SysAlarmTypeServiceImpl extends ServiceImpl<SysAlarmTypeMapper, SysAlarmType>
    implements SysAlarmTypeService{

    @Autowired
    public AlarmConfigService alarmConfigService;


    @Override
    public R<List<SysAlarmType>> getList(@RequestBody SysAlarmType sysAlarmType) {
        LambdaQueryWrapper<SysAlarmType> sysAlarmTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysAlarmTypeLambdaQueryWrapper.like(StringUtils.isNotBlank(sysAlarmType.getAlarmType()),SysAlarmType::getAlarmType,sysAlarmType.getAlarmType());
        sysAlarmTypeLambdaQueryWrapper.eq(SysAlarmType::getDelete,0);
        List<SysAlarmType> sysAlarmTypes = this.baseMapper.selectList(sysAlarmTypeLambdaQueryWrapper);
        return R.ok(sysAlarmTypes);
    }
    @Override
    public R<Page<SysAlarmType>> getPage(Page page, SysAlarmTypePageDto pageDto) {
        LambdaQueryWrapper<SysAlarmType> sysAlarmTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysAlarmTypeLambdaQueryWrapper.like(StringUtils.isNotBlank(pageDto.getAlarmType()),SysAlarmType::getAlarmType,pageDto.getAlarmType());
        sysAlarmTypeLambdaQueryWrapper.eq(SysAlarmType::getDelete,0);
        Page page1 = this.baseMapper.selectPage(page, sysAlarmTypeLambdaQueryWrapper);
        return R.ok(page1);
    }
    @Override
    @Transactional
    public R updateOne(SysAlarmType alarmTypeOne) {
        SysAlarmType sysHisAlarmType = this.baseMapper.selectById(alarmTypeOne.getId());
        LambdaQueryWrapper<AlarmConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AlarmConfig::getAlarmType,sysHisAlarmType.getAlarmType());
        long count = alarmConfigService.count(wrapper);
        if (count > 0){
            return R.fail("更新失败，该类型名称已关联告警");
        }
        if (!Objects.equals(sysHisAlarmType.getAlarmType(), alarmTypeOne.getAlarmType())){
            LambdaQueryWrapper<SysAlarmType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysAlarmType::getAlarmType,alarmTypeOne.getAlarmType());
            Long aLong = this.baseMapper.selectCount(queryWrapper);
            if (aLong > 0){
                return R.fail("编辑，告警类型名称重复");
            }
        }
        //更新数据
        alarmTypeOne.setUpdateDate(LocalDate.now().toString());
        this.baseMapper.updateById(alarmTypeOne);
        return R.ok(null,"更新成功");
    }
    @Override
    public R add(SysAlarmType sysAlarmType) {
        LambdaQueryWrapper<SysAlarmType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysAlarmType::getAlarmType,sysAlarmType.getAlarmType());
        Long aLong = this.baseMapper.selectCount(queryWrapper);
        if (aLong > 0){
            return R.fail("新增失败，已包含该类型");
        }
        sysAlarmType.setStatus(0);
        sysAlarmType.setDelete(0);
        sysAlarmType.setCreateDate(LocalDate.now().toString());
        this.baseMapper.insert(sysAlarmType);
        return R.ok("新增成功");
    }
    @Override
    public R delete(Long id) {
        SysAlarmType sysAlarmType = this.baseMapper.selectById(id);
        LambdaQueryWrapper<AlarmConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AlarmConfig::getAlarmType,sysAlarmType.getAlarmType());
        long count = alarmConfigService.count(wrapper);
        if (count > 0){
            return R.fail("删除失败，告警管理已关联");
        }
        sysAlarmType.setDelete(1);
        this.baseMapper.updateById(sysAlarmType);
        return R.ok("删除成功");
    }


}




